import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { modelService } from "@/mdesign-lib/mdesign-app.umd.js";
import { archService } from "@/mdesign-lib/mdesign-app.umd.js";
import { Shape, GraphModel } from "@/mdesign-lib/mdesign-graph.umd.js";
import { PropertyKey } from "@/mdesign-lib/mdesign-app.umd.js";
import { GanttAddExistingWindow } from "@/config/customSystemConfig/customWindows/ganttAddExistingWindow";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";

export class SpecifyProjectStatusAction extends Action {
  keyName = "specifyProjectStatus" as ActionKey;
  desc = "specifyProjectStatus";

  async run(projectId: string, modelId: string) {
    const typeIds = await modelService
      .getPropertyValueById(projectId, modelId, PropertyKey.InstanceSpecification_classifier)
      .then(data => data as string[]);
    const activeStepKey = typeIds.length ? "setMilestoneStatus" : "";
    const disableStepKey: string[] = [];

    const stepParams = {
      setMilestoneStatus: {
        modelIds: [typeIds[0]],
        currentActualProjectMilestoneId: modelId
      }
    };
    if (typeIds.length) {
      disableStepKey.push("chooseActualProjectMilestoneType");
    }
    await GanttAddExistingWindow.popup(
      {
        projectId,
        title: "项目状态配置",
        stepKeys: ["chooseActualProjectMilestoneType", "setMilestoneStatus"],
        moreBtnCondition: "UAF::Projects::Taxonomy::ProjectMilestone",
        activeStepKey,
        disableStepKey,
        stepParams: JSON.stringify(stepParams)
      },
      async ({ success, result }) => {
        if (!success) return;
        const { modelId: projectMilestoneId, properties } = result;
        archService.modifyProjectStatus(projectId, modelId, properties, projectMilestoneId).finally(() => GanttAddExistingWindow.done());
      }
    );
  }

  onShapeMenuClick(graph: GraphModel, shape: Shape) {
    const modelId = shape.modelId;
    const projectId = app.activeProject!.id;
    this.run(projectId, modelId);
  }
}
